import { mockRequestClient as requestClient } from '#/api/request';

enum Api {
  GetApplicationConfig = '/resource-center/saas/applications/config',
  SaveApplicationConfig = '/resource-center/saas/applications/config',
  GetConfigHistory = '/resource-center/saas/applications/config/history',
  RollbackConfig = '/resource-center/saas/applications/config/rollback',
}

export interface ApplicationConfig {
  applicationId: string;
  basic: {
    name: string;
    version: string;
    description: string;
    environment: string;
    region: string;
  };
  resources: {
    cpu: number;
    memory: number;
    storage: number;
    domain: string;
    port: number;
    replicas: number;
    autoScale: boolean;
  };
  envVars: Array<{
    id: number;
    key: string;
    value: string;
    description: string;
  }>;
  features: Array<{
    key: string;
    name: string;
    enabled: boolean;
    required: boolean;
  }>;
  advancedFeatures: Array<{
    key: string;
    name: string;
    enabled: boolean;
  }>;
}

export interface ConfigHistory {
  id: string;
  applicationId: string;
  version: string;
  config: ApplicationConfig;
  changeDescription: string;
  createdBy: string;
  createdAt: string;
}

// 获取应用配置
export const getApplicationConfig = (applicationId: string) => {
  return requestClient.get(`${Api.GetApplicationConfig}/${applicationId}`);
};

// 保存应用配置
export const saveApplicationConfig = (config: ApplicationConfig) => {
  return requestClient.post(Api.SaveApplicationConfig, config);
};

// 获取配置历史
export const getConfigHistory = (applicationId: string) => {
  return requestClient.get(`${Api.GetConfigHistory}/${applicationId}`);
};

// 回滚配置
export const rollbackConfig = (
  applicationId: string,
  configVersion: string,
) => {
  return requestClient.post(`${Api.RollbackConfig}/${applicationId}`, {
    version: configVersion,
  });
};
